package hhm.framessm.authority.service.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidDataSource;

import hhm.framessm.authority.service.PermissionService;
import hhm.framessm.mapper.TbResourceMapper;
import hhm.framessm.pojo.TbPermission;
import hhm.framessm.pojo.TbResource;

@Service
public class PermissionServiceImpl implements PermissionService {
	@Autowired
	private TbResourceMapper mapper;

	@Autowired
	private DruidDataSource dataSource;

	@Override
	public void insertResource(TbResource resource) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateResource(TbResource resource) {
		// TODO Auto-generated method stub

	}

	@Override
	public List<TbResource> findAllResource() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TbResource findResourceByURI(String uri) {
		
		QueryRunner runner = new QueryRunner(dataSource);
		String sql = "select id,uri,description from tb_resource where uri=?";
		Object[] params = { uri };
		try {
			TbResource resource = (TbResource) runner.query(sql, new BeanHandler(TbResource.class), params);
			if (resource == null)
				return null;
			sql = "select p.id,p.name,p.description from tb_permission p,tb_resource r where r.permission_id=p.id and r.id=?";
			params = new Object[] { resource.getId() };
			TbPermission permission = (TbPermission) runner.query(sql, new BeanHandler(TbPermission.class), params);
			resource.setTbPermission(permission);
			return resource;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;

		}

	}

	@Override
	public Resource findResourceById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteResource(String id) {
		// TODO Auto-generated method stub

	}
	
	

}
